# -*- coding: utf-8 -*-
import os
import ftplib
class FtpClient(object):

    def __init__(self,host,port=21,user='',pwd='',acct='',timeout=5000):
        self.ftp=ftplib.FTP()
        self.host=host
        self.port=port
        self.user=user
        self.passwd=pwd
        self.acct=acct
        self.remotedir=''
        self.file_list=[]
        self.conntimeout=timeout

    def __del__(self):
        self.ftp.close()
        
    def login(self):
        self.ftp.set_pasv(True)
        self.ftp.connect(self.host,self.port,self.conntimeout)
        self.ftp.login(self.user,self.passwd,self.acct)

    def upload_file(self, localfile, remotefile):
        print 'upload_file start'
        if not os.path.isfile(localfile):
            print localfile,' is not a file'
            return False
        print 'upload_file ing'
        file_handler = open(localfile, 'rb')
        self.ftp.storbinary('STOR %s' % remotefile, file_handler)  
        file_handler.close()
        print 'upload_file end'
        return True
    
    def download_file(self, localfile, remotefile):
        if remotefile.startswith('.'):
            self.cwd(FtpClient.remotedir(remotefile))
        file_handler = open(localfile,'wb')
        self.ftp.retrbinary('RETR %s'%(remotefile), file_handler.write)  
        file_handler.close() 
        return localfile

    def download_files(self,local,remote='./'):
        self.cwd(remote)
        self.file_list=[]
        self.ftp.dir(self.get_file_list)
        rtrn=[]
        for file in self.file_list:
            if file[0]=='dir':
                continue
            save=os.path.join(local,file[1])
            self.download_file(save,file[1])
            rtrn.append(save)
        return rtrn

    def del_file(self,remotefile):
        print 'delete ', remotefile
        self.ftp.delete(remotefile)

    def list(self):
        self.ftp.dir(self.get_rawline)
        return self.file_list

    def nlist(self):
        return self.ftp.nlst()
    
    def get_rawline(self, line):  
        self.file_list.append(line) 

    def get_file_list(self, line):   
        ret_arr = []   
        file_arr = self.get_filename(line)   
        self.file_list.append(file_arr)   
         
              
    def get_filename(self, line):  
        pos = line.rfind(':')   
        while(line[pos] != ' '):   
            pos += 1  
        while(line[pos] == ' '):   
            pos += 1  
        type=line[pos] == '<' and 'dir' or 'file'
        while(line[pos] != ' '):   
                pos += 1
        while(line[pos] == ' '):   
                pos += 1
        file_arr = (type, line[pos:])  
        return file_arr

    @staticmethod
    def remotedir(file):
        index=file.rfind('/')
        return file[0:index+1]

    def cwd(self,dir):
        self.remotedir=dir
        self.ftp.cwd(self.remotedir)

    def close(self):
        self.ftp.close()

if __name__=='__main__':
    pass
